package com.example.sixtrainingdemo.view;


import android.util.Log;
import android.view.View;


import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.sixtrainingdemo.R;
import com.example.sixtrainingdemo.adapter.RoundListAdapter;
import com.example.sixtrainingdemo.base.BaseFragment;
import com.example.sixtrainingdemo.contract.RoundContract;
import com.example.sixtrainingdemo.entity.Round;
import com.example.sixtrainingdemo.presenter.RoundPresenter;

import java.util.ArrayList;
import java.util.List;

public class RoundFragment extends BaseFragment implements RoundContract.IRoundView, View.OnClickListener  {

   private RecyclerView mRoundRecyclerView;
   private RoundListAdapter mRecyclerAdapter;
   private RoundContract.IRoundPresenter mRoundPresenter;
   private List<Round> mRoundList;

   @Override
   protected int getLayoutId() {
      return R.layout.fragment_round;
   }

   //这段代码完成了初始化界面组件、配置布局管理器、设置适配器和触发数据加载的整个流程
   @Override
   protected void initView() {
      mRoundPresenter = new RoundPresenter(this, getContext());
      mRoundList = new ArrayList<>();
      mRoundRecyclerView = mView.findViewById(R.id.rv_round);
      //配置RecyclerView的布局管理器为StaggeredGridLayoutManager，这是一种可以创建瀑布流布局的管理器。这里的参数1表示每行显示1个item，VERTICAL表示布局方向为垂直。
      StaggeredGridLayoutManager staggeredGridLayoutManager =
              new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
      mRoundRecyclerView.setLayoutManager(staggeredGridLayoutManager);
      mRecyclerAdapter = new RoundListAdapter(mRoundList);
      mRoundRecyclerView.setAdapter(mRecyclerAdapter);
      mRoundPresenter.getRoundList();
   }

   @Override
   protected void initListener() {

   }

   @Override
   public void onResume() {
      super.onResume();
      mRoundPresenter.getRoundList();
   }

   //显示列表
   @Override
   //接收一个List<Round>类型的参数roundList，代表了一组新的轮次数据。然后，将这个新的数据列表赋值给成员变量mRoundList，这一步骤更新了应用内部用于存储轮次数据的列表。
   public void showRoundList(List<Round> roundList) {
      mRoundList = roundList;
      Log.d("", "showRoundList " + mRoundList.size());
      //调用RecyclerView的适配器mRecyclerAdapter的updateData方法，将最新的mRoundList数据传递给适配器。
      mRecyclerAdapter.updateData(mRoundList);
   }

   @Override
   public void onClick(View v) {
//      int id = v.getId();
//      if (id == R.id.gosale) {
//         Intent intent = new Intent(mContext, Main1Activity.class);
//         startActivity(intent);
//      }
   }

}
